package com.wyy.mall.tiny.controller;

import com.wyy.mall.tiny.common.api.CommonPage;
import com.wyy.mall.tiny.common.api.CommonResult;
import com.wyy.mall.tiny.mbg.model.PmsBrand;
import com.wyy.mall.tiny.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description: 品牌管理控制器
 * created by 伍猷煜 on 2021/5/7 22:16 星期五
 */
@Api(tags = "PmsBrandController", description = "商品品牌管理")
@Controller
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired // 变量注入，建议用set注入或构造器注入
    // @Qualifier(value = "pmsBrandServiceImpl") // 如果有多个实现类，通过这个指定
    private PmsBrandService mPmsBrandService;

    private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);

    @ApiOperation("获取所有品牌列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    // @RequestMapping(value = "listAll", method = RequestMethod.GET) 这个也可以不加斜杠
    @ResponseBody
    public CommonResult<List<PmsBrand>> getBrandList() {
        return CommonResult.success(mPmsBrandService.listAllBrands());
    }

    @ApiOperation("分页获取品牌")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                              @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        CommonPage<PmsBrand> commonPage = CommonPage.restPage(mPmsBrandService.listBrands(pageNum, pageSize));
        return CommonResult.success(commonPage);
    }

    @ApiOperation("获取指定id的品牌")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsBrand> brand(@PathVariable("id") Long id) {
        return CommonResult.success(mPmsBrandService.getBrand(id));
    }

    @ApiOperation("添加品牌")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    // 创建品牌 前台给id是没有效果的，数据库会给记录生成id
    public CommonResult createBrand(@RequestBody PmsBrand pmsBrand) {
        int count = mPmsBrandService.createBrand(pmsBrand);
        if (count == 1) {
            LOGGER.debug("createdBrand success:{}", pmsBrand);
            return CommonResult.success(pmsBrand);
        }
        else {
            LOGGER.debug("createdBrand failed:{}", pmsBrand);
            return CommonResult.failed("创建品牌失败");
        }
    }

    @ApiOperation("更新指定id品牌信息")
    // @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    // PUT测试下效果 结果是一样的，客户端需要用PUT方式提交
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult updateBrand(@PathVariable("id") Long id, @RequestBody PmsBrand pmsBrand, BindingResult result) {
        int count = mPmsBrandService.updateBrand(id, pmsBrand);
        if (count == 1) {
            LOGGER.debug("updateBrand success:{}", pmsBrand);
            return CommonResult.success(pmsBrand);
        } else {
            LOGGER.debug("updateBrand failed:{}", pmsBrand);
            return CommonResult.failed("修改品牌失败!");
        }
    }

    @ApiOperation("删除指定id的品牌")
    // @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    // DELETE测试下效果 结果是一样的，客户端需要用DELETE方式提交
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult deleteBrand(@PathVariable("id") Long id) {
        int count = mPmsBrandService.deleteBrand(id);
        if (count == 1) {
            LOGGER.debug("deleteBrand success :id = {}", id);
            return CommonResult.success(null);
        } else {
            LOGGER.debug("deleteBrand failed :id = {}", id);
            return CommonResult.failed("删除品牌失败！");
        }
    }

}
